from django.urls import path, include
from rest_framework.routers import DefaultRouter
from . import views
from . import zabbix_dashboard_api

# 创建路由器
router = DefaultRouter()
router.register(r'zabbix-servers', views.ZabbixServerViewSet)
# Grafana服务器路由已移除
router.register(r'monitoring-hosts', views.MonitoringHostViewSet)
router.register(r'alerts', views.MonitoringAlertViewSet)
router.register(r'alert-rules', views.AlertRuleViewSet)
router.register(r'notification-channels', views.NotificationChannelViewSet)
router.register(r'alert-notifications', views.AlertNotificationViewSet)
router.register(r'email-configs', views.EmailConfigViewSet)

urlpatterns = [
    # SSE实时告警推送 - 必须在router之前，避免与alerts路由冲突
    path('sse/alerts/stream/', views.alert_stream, name='alert-stream'),
    path('sse/status/', views.sse_status, name='sse-status'),
    path('sse/test-push/', views.test_alert_push, name='test-alert-push'),
    path('sse/config/', views.SSEConfigView.as_view(), name='sse-config'),
    path('test-alert-push/', views.test_alert_push, name='test-alert-push-alt'),

    # API路由
    path('', include(router.urls)),

    # 连接测试
    path('zabbix-servers/<int:pk>/test-connection/',
         views.ZabbixServerViewSet.as_view({'post': 'test_connection'}),
         name='zabbix-test-connection'),

    # 模板导出导入删除
    path('zabbix-servers/<int:pk>/export-template/<str:template_id>/',
         views.ZabbixServerViewSet.as_view({'get': 'export_template'}),
         name='zabbix-export-template'),
    path('zabbix-servers/<int:pk>/batch-export-templates/',
         views.ZabbixServerViewSet.as_view({'post': 'batch_export_templates'}),
         name='zabbix-batch-export-templates'),
    path('zabbix-servers/<int:pk>/import-template/',
         views.ZabbixServerViewSet.as_view({'post': 'import_template'}),
         name='zabbix-import-template'),
    path('zabbix-servers/<int:pk>/batch-import-templates/',
         views.ZabbixServerViewSet.as_view({'post': 'batch_import_templates'}),
         name='zabbix-batch-import-templates'),
    path('zabbix-servers/<int:pk>/delete-template/<str:template_id>/',
         views.ZabbixServerViewSet.as_view({'delete': 'delete_template'}),
         name='zabbix-delete-template'),
    # Grafana连接测试路由已移除

    # 同步操作
    path('sync/hosts-to-zabbix/', views.sync_hosts_to_zabbix, name='sync-hosts-to-zabbix'),
    path('sync/alerts-from-zabbix/', views.sync_alerts_from_zabbix, name='sync-alerts-from-zabbix'),
    path('sync/monitoring-status-to-cmdb/', views.sync_monitoring_status_to_cmdb, name='sync-monitoring-status-to-cmdb'),
    path('cleanup/monitoring-hosts/', views.cleanup_monitoring_hosts, name='cleanup-monitoring-hosts'),
    path('quick-associate/', views.quick_associate_host, name='quick-associate-host'),

    # 监控数据
    path('dashboard/overview/', views.monitoring_overview, name='monitoring-overview'),
    path('dashboard/host-status/', views.host_monitoring_status, name='host-monitoring-status'),
    path('hosts/<str:host_id>/metrics/', views.get_host_detailed_metrics, name='host-detailed-metrics'),
    path('hosts/<str:host_id>/configuration/', views.host_configuration, name='host-configuration'),
    path('hosts/items/', views.get_host_items, name='get-host-items'),
    path('items/update-status/', views.update_item_status, name='update-item-status'),
    path('items/delete/', views.delete_item, name='delete-item'),
    path('templates/', views.available_templates, name='available-templates'),
    path('hostgroups/', views.available_hostgroups, name='available-hostgroups'),
    path('test/simple/', views.test_simple_api, name='test-simple-api'),

    # 告警管理
    path('acknowledge-alert/', views.acknowledge_alert, name='acknowledge-alert'),
    path('resolve-alert/', views.resolve_alert, name='resolve-alert'),
    path('alert-workflow/', views.get_alert_workflow, name='alert-workflow'),
    path('batch-alert-action/', views.batch_alert_action, name='batch-alert-action'),

    # 通知管理
    path('notification-rules/', views.notification_rules, name='notification-rules'),
    path('retry-failed-notifications/', views.retry_failed_notifications, name='retry-failed-notifications'),
    path('notification-statistics/', views.notification_statistics, name='notification-statistics'),

    # 监控项管理
    path('templates/search/', views.search_templates, name='search-templates'),
    path('template-items/', views.get_template_items, name='template-items'),

    # Zabbix仪表板API
    path('dashboard/host-groups/', zabbix_dashboard_api.get_host_groups, name='dashboard-host-groups'),
    path('dashboard/hosts/', zabbix_dashboard_api.get_hosts_by_group, name='dashboard-hosts'),
    path('dashboard/graphs/', zabbix_dashboard_api.get_host_graphs, name='dashboard-graphs'),
    path('dashboard/graph-data/', zabbix_dashboard_api.get_graph_data, name='dashboard-graph-data'),
    path('dashboard/create-graph/', zabbix_dashboard_api.create_graph, name='dashboard-create-graph'),
    path('dashboard/host-items/', zabbix_dashboard_api.get_host_items, name='dashboard-host-items'),
]
